package reflect;

import reflect.annotation.AutoRunClass;
import reflect.annotation.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test5 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        File dir=new File(Test5.class.getResource(".").toURI());
        File[] subs=dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub:subs) {
            String className=sub.getName().substring(0,sub.getName().indexOf("."));
            Class cls=Class.forName(Test5.class.getPackage().getName()+"."+className);
            if (cls.isAnnotationPresent(AutoRunClass.class)){
                Object o = cls.newInstance();
                Method[] methods=cls.getDeclaredMethods();
                for (Method m:methods){
                    if (m.isAnnotationPresent(AutoRunMethod.class)){
                        AutoRunMethod a = m.getAnnotation(AutoRunMethod.class);
                        int num= a.value();
                        for (int i=0;i<num;i++){
                            m.setAccessible(true);
                            m.invoke(o);
                        }

                    }
                }
            }
        }
    }
}
